//切换工作目录与导入初始数据
cd "D:\Document\doing\1、资本市场主动与被动信息披露——基于股份回购与机构调研"
/*
use "年度数据.dta",clear
save "年度数据.dta",replace
    */
use "年度数据.dta",clear

//删除部分变量
drop if 所属东财行业指数 == "金融" | 所属东财行业指数 == "房地产"
drop if Year<2010
drop Beta2 Cor2 NonSysRisk2 Rsq2 ARsq2    //总市值加权数据
drop Amount 第一大股东持股比例 OI NI Ail_rev Opncf_rev Cfadqrt Dps_at DiltEPS Rev_ps Opic_ps Apic_ps Sr_ps Udp_ps Rten_ps Ncf_ps Ebit_ps 个人来访接待量 个人来访占比 其他来访占比

drop REV Revrt Nirt Lbrt Ppert Ivtyto Curastto Fxastto Totastto Selexprt Ohexprt Fiexprt Cstexppm Csopindex Ascscvrt Csdivgrml BscEPS 来访接待量 机构来访接待量 机构来访占比 证券公司调研家数 基金公司调研家数 信托公司调研家数 财务公司调研家数 银行调研家数 私募调研家数 保险公司调研家数 资管公司调研家数 FShrhN BShrhN IShrhN SShrhN QFIIShrhN OShrhN Estbdt HLTFA CA TLE Actrcbto Wkcaptto Ncurastto Actpayto Roepc_2 第一产业增加值 第二产业增加值 第三产业增加值 人均国内生产总值

//缺失值处理
mvencode SecSurTim FunSurTim TruSurTim FinSurTim BanSurTim PriSurTim InsuSurTim CapSurTim, mv(0) override
mvencode FunSha FunAssMan SecSha SecAssMan QFIISha SocInsSha SupPenSha InsSha InsAssMan TruSha TruAssMan FiaSha BanSha GenSha NonFiaSha PenSha, mv(0) override
mvencode FCSRt BCSRt ICSRt SCSRt QFIICSRt OCSRt, mv(0) override
mvencode Pubsub, mv(0) override

//变量加工
gen InsSur= SecSurTim + FunSurTim + TruSurTim + FinSurTim + BanSurTim + PriSurTim + InsuSurTim + CapSurTim
label variable InsSur "机构调研次数"
order InsSur, before(SecSurTim)
gen Size = ln(AT) , after(AT)
gen RepurAmoR = 100*RepurAmount/Mos ,after(RepurAmount)
gen RepurMonR = 100*RepurMon/MarketValueA2 ,after(RepurMon)
gen SOERepurAmoR = RepurAmoR*SOE
gen SOERepurTime = RepurTime*SOE
order SOERepurAmoR, after(RepurAmoR)
gen Policy=1 if Year>2017
replace Policy=0 if Year<=2017
gen RepurAmoR_P = RepurAmoR*Policy
gen InsSur_P = InsSur*Policy
gen Top3Sha = Sha1+Sha2+Sha3
gen Top4_10Sha = Sha4+Sha5+Sha6+Sha7+Sha8+Sha9+Sha10
order Top3Sha Top4_10Sha,after(Top10Sha)

//标准化数据

center RepurAmoR RepurMonR,addtolabel(标准化) inplace standardize

//缩尾
winsor2 Turnover Beta1 ARsq1 InstiSha Top10Sha RepurTime RepurAmount RepurMon RepurAmoR RepurMonR InsSur,replace cuts(1,99)
//删除控制变量缺失观测
drop if Year==. | Size==. |  Aslbrt==. |  Roa_2==. |  BSEPS==. |  EV2ToEBITDA==. |  M2G==. |  MacroEcoAcco==. 
//面板数据设定
sort Scode Year
xtset Scode Year



/*
tab2 Year RepurTime
tab year ,gen(dumt) (tab命令用于列示变量year的组类别，选项gen（dumt）用于生产一个以dumt开头的年度虚拟变量) 
xtdescribe 
xtsum 回购次数 回购数量 回购金额
    */

//定义全局变量
global DepVar Turnover Beta1 ARsq1 InstiSha Top10Sha    //被解释变量
global Control Year Size Aslbrt Roa_2 BSEPS EV2ToEBITDA M2G MacroEcoAcco    //控制变量
global XtOpt " ,fe vce(robust)"    //面板数据回归固定效应选项
global UseVar Turnover Beta1 ARsq1 InstiSha Top3Sha Top4_10Sha InsSur RepurAmoR $Control    //所有变量
order Size Aslbrt Roa_2 BSEPS EV2ToEBITDA M2G MacroEcoAcco , after(Year)



/*+++++++++++++++++++++++++++++描述性统计++++++++++++++++++++++++++++++++*/

//描述性统计

bysort Year RepurTime : sum Turnover  Liquidility
bysort Year RepurTime : sum  Beta1 ARsq1

sum2docx $UseVar using 描述性统计.docx , replace stats(N mean(%9.2f) sd(%9.2f) median(%9.2f) min(%9.2f) max(%9.2f)) title("变量描述性统计") font("宋体",10,"black") note("数据来源：东方财富Choice数据、CSMAR数据库、中国研究数据服务平台(CNRDS)") pagesize(A4)

corr2docx $UseVar  using 描述性统计.docx , append pearson(pw) title("变量相关系数") font("Microsoft Himalaya",8,"black") note("数据来源：东方财富Choice数据、CSMAR数据库、中国研究数据服务平台(CNRDS)") pagesize(A4) star(* 0.10 ** 0.05 *** 0.01) landscape

pwcorr  $UseVar ,star(0.05) sig

/*++++++++++++++++++++++++++++对资本市场的影响+++++++++++++++++++++++++++++++*/

//检验
eststo estfe : xtreg Turnover RepurAmoR InsSur $Control ,fe 
xttest3    //3、异方差检验第一步，原假设：同方差，需要检验模型中是否存在组间异方差,需要使用xttest3命令
eststo estre : xtreg Turnover RepurAmoR InsSur $Control ,re 
xttest0    //2、检验时间效应，可以看出，LM检验得到的P值为0.0000，表明随机效应非常显著。可见，随机效应模型也优于混合OLS模型
xttest1    //6、RE模型序列相关检验
hausman estfe estre    //1、检验固定与随机效应，不满足随机效应假设
xtgls Turnover 回购数量比例 机构调研次数 $Control ,panels(heteroskedastic)    //4、异方差检验第二步，进一步以获得参数的GLS估计量，上述结果是采用两步获得，即，先采用OLS估计不考虑异方差的模型，进而利用其残差计算。并最终得到FGLS估计量。
xtserial Turnover 回购数量比例 机构调研次数 $Control    //5、FE模型的序列相关检验,存在自相关
/*动态面板
xtabond    //动态面板估计命令，差分GMM
xtdpdsys    //动态面板估计命令，系统GMM
estat sargan    //内生性检验
    */
xtivreg    //面板工具变量

tab Year , gen(Year_dummy)
rename Year_dummy1 Year2010
rename Year_dummy2 Year2011
rename Year_dummy3 Year2012
rename Year_dummy4 Year2013
rename Year_dummy5 Year2014
rename Year_dummy6 Year2015
rename Year_dummy7 Year2016
rename Year_dummy8 Year2017
rename Year_dummy9 Year2018
rename Year_dummy10 Year2019
gen InsSur2010 = InsSur*Year2010
gen InsSur2011 = InsSur*Year2011
gen InsSur2012 = InsSur*Year2012
gen InsSur2013 = InsSur*Year2013
gen InsSur2014 = InsSur*Year2014
gen InsSur2015 = InsSur*Year2015
gen InsSur2016 = InsSur*Year2016
gen InsSur2017 = InsSur*Year2017
gen InsSur2018 = InsSur*Year2018
gen InsSur2019 = InsSur*Year2019

gen RepurAmoR2010 = RepurAmoR*Year2010
gen RepurAmoR2011 = RepurAmoR*Year2011
gen RepurAmoR2012 = RepurAmoR*Year2012
gen RepurAmoR2013 = RepurAmoR*Year2013
gen RepurAmoR2014 = RepurAmoR*Year2014
gen RepurAmoR2015 = RepurAmoR*Year2015
gen RepurAmoR2016 = RepurAmoR*Year2016
gen RepurAmoR2017 = RepurAmoR*Year2017
gen RepurAmoR2018 = RepurAmoR*Year2018
gen RepurAmoR2019 = RepurAmoR*Year2019


eststo xtres1 : xtreg Top10Sha InsSur $Control $XtOpt

eststo xtres1 : xtreg Top4_10Sha RepurAmoR  $Control $XtOpt


Top3Sha Top4_10Sha Top10Sha

//合并分析

quietly eststo xtres1 : xtreg Turnover InsSur $Control $XtOpt
quietly eststo xtres2 : xtreg Turnover RepurAmoR $Control $XtOpt
reg2docx xtres1 xtres2 using 合并资本市场分析.docx,replace note("数据来源：东方财富Choice数据、CSMAR数据库、中国研究数据服务平台(CNRDS)") t(%9.2f) scalars(N r2(%9.2f)) star(* 0.10 ** 0.05 *** 0.01)  depvar b(%6.4f) title(股票流动性的影响) order(InsSur RepurAmoR $Control)

quietly eststo xtres1 : xtreg Beta1 InsSur $Control $XtOpt
quietly eststo xtres2 : xtreg Beta1 RepurAmoR $Control $XtOpt
quietly eststo xtres3 : xtreg ARsq1 InsSur $Control $XtOpt
quietly eststo xtres4 : xtreg ARsq1 RepurAmoR  $Control $XtOpt
reg2docx xtres1 xtres2 xtres3 xtres4 using 合并资本市场分析.docx,append note("数据来源：东方财富Choice数据、CSMAR数据库、中国研究数据服务平台(CNRDS)") t(%9.2f) scalars(N r2(%9.2f)) star(* 0.10 ** 0.05 *** 0.01)  depvar b(%6.4f) title(股票风险的影响) order(InsSur RepurAmoR $Control)

quietly eststo xtres1 : xtreg InstiSha InsSur $Control $XtOpt
quietly eststo xtres2 : xtreg InstiSha RepurAmoR $Control $XtOpt
quietly eststo xtres3 : xtreg Top3Sha InsSur $Control $XtOpt
quietly eststo xtres4 : xtreg Top3Sha RepurAmoR  $Control $XtOpt
quietly eststo xtres5 : xtreg Top4_10Sha InsSur $Control $XtOpt
quietly eststo xtres6 : xtreg Top4_10Sha RepurAmoR  $Control $XtOpt
reg2docx xtres1 xtres2 xtres3 xtres4 xtres5 xtres6 using 合并资本市场分析.docx,append note("数据来源：东方财富Choice数据、CSMAR数据库、中国研究数据服务平台(CNRDS)") t(%9.2f) scalars(N r2(%9.2f)) star(* 0.10 ** 0.05 *** 0.01)  depvar b(%6.4f) title(投资者结构的影响) order(InsSur RepurAmoR $Control)



foreach depv in $DepVar {
quietly eststo xtres1 : xtreg `depv' InsSur $Control $XtOpt
quietly eststo xtres2 : xtreg `depv' SecSurTim $Control $XtOpt
quietly eststo xtres3 : xtreg `depv' FunSurTim $Control $XtOpt
reg2docx xtres1 xtres2 xtres3 using 机构调研资本市场分析.docx,append note("数据来源：东方财富Choice数据、中国研究数据服务平台(CNRDS)") t(%9.2f) scalars(N r2(%9.2f)) star(* 0.10 ** 0.05 *** 0.01)  depvar noconstant b(%6.4f) title(机构调研对`depv'的影响) order(InsSur SecSurTim FunSurTim $Control)
}

foreach depv in $DepVar {
quietly eststo xtres1 : xtreg `depv' RepurTime $Control $XtOpt
quietly eststo xtres2 : xtreg `depv' RepurAmoR $Control $XtOpt
quietly eststo xtres3 : xtreg `depv' RepurMonR $Control $XtOpt
reg2docx xtres1 xtres2 xtres3 using 回购股份资本市场分析.docx,append note("数据来源：东方财富Choice数据、中国研究数据服务平台(CNRDS)") t(%9.2f) scalars(N r2(%9.2f)) star(* 0.10 ** 0.05 *** 0.01)  depvar noconstant b(%6.4f) title(回购股份对`depv'的影响) order(RepurTime RepurAmoR RepurMonR $Control)
}




